<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/assets/layui/css/layui.css">
</head>

<body>
  <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
    <legend>修改密码</legend>
  </fieldset>

  <form class="layui-form" action="" lay-filter="example">
    <div class="layui-form-item">
      <label class="layui-form-label">请输入旧密码</label>
      <div id="box"></div>
      <div class="layui-input-block">

        <input type="text" name="password" id="password" lay-verify="title" autocomplete="off" placeholder="请输入密码"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">请输入新密码</label>
      <div class="layui-input-block">
        <input type="password" name="Newpassword" id="Newpassword" placeholder="请输入新密码" autocomplete="off"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">再次确认新密码</label>
      <div class="layui-input-block">
        <input type="password" name="Npassword" id="Npassword" placeholder="请确认密码" autocomplete="off"
          class="layui-input">
      </div>
    </div>




    <div class="layui-form-item">
      <div class="layui-input-block">
        <button type="button" class="layui-btn" lay-submit="" id="btn" lay-filter="demo1">立即提交</button>
      </div>
    </div>
  </form>
</body>
<script src="/assets/layui/layui.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/js-cookie/3.0.1/js.cookie.min.js"></script>
<script>
  let contro = false
  $('#password').blur(function () {
    let user = JSON.parse(Cookies.get('userInfo'))
    const { username } = user;
    $.post('/isUser', {
      username,
      password: $(this).val(),
    }).then(res => {
      if (res?.id) {
        contro = true;
        Cookies.set('id', res.id)
      } else {
        contro = false;
        Cookies.set('id')
      }
    })
  })

  $('#btn').click(function () {
    if (contro) {
      let Newpassword = $('#Newpassword').val()
      let Npassword = $('#Npassword').val()
      if (Newpassword === Npassword) {
        let id = Cookies.get('id')
        id = parseInt(id)
        $.post('/eaitpsd', {
          id,
          password: Npassword.val()
        })
        parent.location.href = '/login'
      } else {
        alert('两个密码不一致')
      }
    }else {
      alert('fail')
    }

  })

</script>

</html>